package com.example.springboot.MyBatis.controller;

import com.example.springboot.MyBatis.Vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@Component
@RestController
@RequestMapping("/test03")
public class Test03 {

    //要获取的配置项名称和变量名不用一致
    @Value("${string1}")
    private String String2;

    @Value("${likes[1]}")
    private String likes1_1;

    @Value("${users[1].name}")
    private String users_1_name;

    @Value("$(为小括号时，为直接复制本字符串)")
    private String String2ss;

    //使用自动装配装配所有数据
    @Autowired
    private Environment env;

    //自动装配制定数据
    @Autowired
    private User user;

    @GetMapping("/t1")
    @ResponseBody
    public String getSettings(){
        return "get Set is \n"
                + "string1 : " + String2 + "\n"
                + "likes[1] : " + likes1_1 + "\n"
                + "users[1].name : " + users_1_name + "\n"
                + "String2ss : " + String2ss + "\n"
                ;
    }

    @GetMapping("/t2")
    @ResponseBody
    public String getAutowired(){
        return "getAutowired is "+
                "users[1].name :"+env.getProperty("users[1].name");
    }

    @GetMapping("/t3")
    @ResponseBody
    public String getConfigUser(){
        return "user is " + user.toString();
    }

}
